package leetcode;

import java.util.*;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-13 14:34
 **/
public class LeetCode49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>(16);
        for (String str : strs) {
            char[] chs = str.toCharArray();
            int[] count = new int[26];
            for (int i = 0; i < str.length(); i++) {
                ++count[str.charAt(i) - 'a'];
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < count.length; i++) {
                if (count[i] != 0) {
                    builder.append((char) ('a' + i));
                    builder.append(count[i]);
                }
            }
            String key = builder.toString();
            //map中不存在key,就创建key,value默认为new ArrayList<>(),computeIfAbsent方法返回value,value.add(str)
            map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
        }
        return new ArrayList<>(map.values());
    }

    //排序
    /*public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>(16);
        for (String str : strs) {
            char[] chs = str.toCharArray();
            Arrays.sort(chs);
            String key = new String(chs);
            //map中不存在key,就创建key,value默认为new ArrayList<>(),computeIfAbsent方法返回value,value.add(str)
            map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
        }
        return new ArrayList<>(map.values());
    }*/

    public static void main(String[] args) {
        int[] arr1 = new int[]{1};
        int[] arr2 = new int[]{1};
        System.out.println(Arrays.equals(arr1, arr2));
        Map<int[], String> map = new HashMap<>();
        map.put(arr1, "1");
        map.put(arr2, "2");
        for (int[] ints : map.keySet()) {
            System.out.println(Arrays.toString(ints));
        }
    }

}
